home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / 3d game controls / source / event.c < prev    next >
Encoding:
Text File  |  1996-06-29  |  6.7 KB  |  315 lines

  1. //--------------------------------------------------------------------------------------------
  2. //  Event Handling Code
  3. //
  4. //      by Philip McBride
  5. //        parts freely taken from Apple DTS sample code
  6. //
  7. //--------------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include "GameControls.h"
  11. #include "extern.h"
  12. #include "event.h"
  13. #include "AEVT.h"
  14. #include "inits.h"
  15. #include "document.h"
  16. #include "file.h"
  17. #include "draw.h"
  18.  
  19. //--------------------------------------------------------------------------------------------
  20. //  Event Loop
  21. //
  22. void EventLoop(void)
  23. {    
  24.     EventRecord        theEvent;
  25.     RgnHandle        theMouseRgn;
  26.  
  27.     theMouseRgn = NewRgn();
  28.  
  29.      while (! gQuit) {
  30.         if (WaitNextEvent(everyEvent, &theEvent, 0L, theMouseRgn))
  31.             DoEvent(&theEvent);
  32.     }
  33.     
  34.     DisposeRgn(theMouseRgn);
  35. }
  36.  
  37. //--------------------------------------------------------------------------------------------
  38. //  Handle any Event
  39. //
  40. void DoEvent(EventRecord *theEvent)
  41. {    
  42.     short               thePart;
  43.     WindowPtr            theWindow ;
  44.     Rect                screenRect;
  45.     Point                aPoint = {100, 100};
  46.  
  47.     switch(theEvent->what) {
  48.         case nullEvent:
  49.             break;
  50.             
  51.         case mouseDown:
  52.             thePart = FindWindow( theEvent->where, &theWindow );
  53.             
  54.             switch( thePart ) {
  55.                 case inMenuBar: 
  56.                     MyAdjustMenus() ;
  57.                     HandleMenuCommand(MenuSelect(theEvent->where));
  58.                     break;
  59.                 
  60.                 case inDrag:
  61.                     screenRect = (**GetGrayRgn()).rgnBBox;
  62.                     DragWindow( theWindow, theEvent->where, &screenRect );
  63.                     break ;
  64.             
  65.                 case inContent:
  66.                     if (theWindow != FrontWindow())
  67.                         SelectWindow( theWindow );
  68.                     else
  69.                         HandleMouseDown(theEvent);
  70.                     break ;
  71.             
  72.                 case inGoAway:
  73.                     if (TrackGoAway( theWindow, theEvent->where )) {
  74.                         MyCloseDocument(MyGetDocumentFromWindow(theWindow));
  75.                     }
  76.                     break ;
  77.                     
  78.                 default:
  79.                     break ;
  80.             }
  81.             break ;
  82.             
  83.         case updateEvt:
  84.             theWindow = FrontWindow();
  85.             BeginUpdate( theWindow );
  86.             MyUpdateScreen(MyGetDocumentFromWindow(theWindow));
  87.             EndUpdate( theWindow );
  88.             break ;
  89.             
  90.         case mouseUp:
  91.             break;
  92.             
  93.         case keyDown:
  94.         case autoKey:
  95.             HandleKeyPress(theEvent);
  96.             break;
  97.             
  98.         case diskEvt:
  99.             if ( HiWrd(theEvent->message) != noErr ) 
  100.                 (void) DIBadMount(aPoint, theEvent->message);
  101.             break;
  102.  
  103.         case activateEvt:
  104.             break;
  105.         case osEvt:
  106.             break;
  107.             
  108.         case kHighLevelEvent:
  109.             DoHighLevelEvent(theEvent);
  110.             break;
  111.     }
  112. }
  113.  
  114. //--------------------------------------------------------------------------------------------
  115. //  Handle Key Down
  116. //
  117. void HandleKeyPress(EventRecord *theEvent)
  118. {
  119.     char            key;
  120.     WindowPtr        theWindow;
  121.  
  122.     key = theEvent->message & charCodeMask;
  123.     theWindow = FrontWindow();
  124.  
  125.     // If command key down, then it's a menu command,
  126.     // otherwise we're moving the camera with the keyboard.
  127.     if ( theEvent->modifiers & cmdKey ) {
  128.         HandleMenuCommand(MenuKey(key));
  129.     } else {
  130.         MyDoKeyMove(theWindow, theEvent, key);
  131.     }
  132. }
  133.  
  134. //--------------------------------------------------------------------------------------------
  135. //  Handle Mouse Down
  136. //
  137. void HandleMouseDown(EventRecord *theEvent)
  138. {
  139.     WindowPtr        theWindow;
  140.     
  141.     theWindow = FrontWindow();
  142.     MyDoMouseMove(theWindow, theEvent);
  143. }
  144.  
  145. //--------------------------------------------------------------------------------------------
  146. //  Handle the About Application Event
  147. //
  148.  
  149. void HandleAboutApp( void )
  150. {
  151.     ModalFilterUPP         theProc ;
  152.     DialogPtr            theDialog ; 
  153.     short                itemHit ;
  154.  
  155.     theDialog = GetNewDialog ( kMyAboutDialogID, nil, (WindowPtr)-1 );
  156.     
  157.     // these two lil' snappers are system 7 only
  158.     // so if you use them, check before!!
  159.     // in this app we will only run on Power
  160.     // Macintosh, so we don't check
  161.     
  162.     GetStdFilterProc( &theProc ) ;
  163.     SetDialogDefaultItem(theDialog, ok) ;
  164.     
  165.     // put the dialog up and loop 'til
  166.     // the user hits the OK button
  167.     
  168.     do {
  169.         ModalDialog ( theProc, &itemHit );
  170.     } while( itemHit != ok ) ;
  171.     
  172.     DisposDialog ( theDialog );
  173. }
  174.  
  175. //--------------------------------------------------------------------------------------------
  176. //  Handle an Open Document
  177. //
  178. OSErr HandleOpenDoc(FSSpec *theFile)
  179. {
  180.     OSErr                err ;
  181.     DocumentPtr            theDocument ;
  182.     WindowPtr            theWindow ;
  183.  
  184.     // Create a new document and associated window.
  185.     theDocument = MyNewDocument();
  186.     theWindow = (WindowPtr)theDocument->theWindow ;
  187.     
  188.     // Open the file.
  189.     err = MyOpenFile( theFile, theDocument ) ;
  190.     if (err == noErr) {
  191.         MyAdjustMenus() ;
  192.         MyUpdateScreen(theDocument) ;
  193.     } else {
  194.         MyCloseDocument(theDocument);
  195.     }
  196.  
  197.     return err ;
  198. }
  199.  
  200.  
  201. //--------------------------------------------------------------------------------------------
  202. //  Handle a Menu Command
  203. //
  204. void HandleMenuCommand(long menuResult)
  205. {
  206.     short                menuID;
  207.     short                menuItem;
  208.     Str255                daName;
  209.  
  210.     short                numTypes = 2 ;
  211.     SFTypeList            myTypes = { '3DMF', 'TEXT' } ;
  212.     
  213.     DocumentPtr            theDocument ;
  214.     TQ3ViewObject         theView ;
  215.     WindowPtr            theWindow ;
  216.         
  217.     StandardFileReply    theSFReply ;
  218.  
  219.     menuID = HiWrd(menuResult);
  220.     menuItem = LoWrd(menuResult);
  221.     
  222.     switch ( menuID ) {
  223.         //
  224.         //--------------------------------------------------------------------------    
  225.         //
  226.         case mApple:
  227.             switch ( menuItem ) {
  228.  
  229.                 case iAbout:
  230.                     HandleAboutApp() ;    
  231.                     break ;
  232.                                 
  233.                 default:
  234.                     GetItem(GetMHandle(mApple), menuItem, daName);
  235.                     (void) OpenDeskAcc(daName);
  236.                     break;
  237.             }
  238.             break;
  239.         //
  240.         //--------------------------------------------------------------------------    
  241.         //
  242.         case mFile:
  243.             switch ( menuItem ) {
  244.                 case iOpen:
  245.                     // Get the file name to open
  246.                     StandardGetFile( nil, numTypes, myTypes, &theSFReply ) ;
  247.                     
  248.                     // If the user didn't cancel, open the file.
  249.                     if(theSFReply.sfGood)
  250.                         HandleOpenDoc(&theSFReply.sfFile) ;
  251.  
  252.                     break ;
  253.                 
  254.                 case iClose:
  255.                     MyCloseDocument( MyGetDocumentFromWindow ( FrontWindow() ));
  256.                     break ;
  257.                     
  258.                 case iQuit:
  259.                     MySendQuitApp();
  260.                     break;
  261.             }
  262.             break;
  263.             
  264.             
  265.         //
  266.         //--------------------------------------------------------------------------    
  267.         //
  268.         case mView:
  269.             
  270.             theWindow = FrontWindow() ;
  271.             
  272.             if( theWindow == nil )
  273.                 break ;
  274.                 
  275.             theDocument = MyGetDocumentFromWindow( theWindow ) ;
  276.             theView = theDocument->theView ;
  277.             
  278.             switch(menuItem)
  279.             {
  280.                 case iWireframe:
  281.                     {
  282.                         TQ3RendererObject        myRenderer;
  283.     
  284.                         // set the renderer to wireframe
  285.                         myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
  286.                         Q3View_SetRenderer(theView, myRenderer);
  287.                         
  288.                         Q3Object_Dispose( myRenderer ) ;
  289.                     }
  290.                     break;
  291.                     
  292.                 case iInteractive:
  293.                     {
  294.                         TQ3RendererObject        myRenderer;
  295.     
  296.                         // set the renderer to use the interactive renderer
  297.                         myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  298.                         Q3View_SetRenderer(theView, myRenderer);
  299.  
  300.                         Q3Object_Dispose( myRenderer ) ;
  301.                     }
  302.                     break;
  303.                     
  304.                 default:
  305.                     break;
  306.             }
  307.             
  308.             // Update screen.
  309.             MyUpdateScreen(theDocument);
  310.             break;
  311.  
  312.     }
  313.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  314. }
  315.